<?php require_once("includes/connection.php"); ?>

<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
      $cd = strtotime($givendate);
      $newdate = date('Y-m-d', mktime(date('h',$cd),
    date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
    date('d',$cd)+$day, date('Y',$cd)+$yr));
      return $newdate;
              }
function sub_date($givendate,$day=0,$mth=0,$yr=0) {
      $cd = strtotime($givendate);
      $newdate = date('Y-m-d', mktime(date('h',$cd),
    date('i',$cd), date('s',$cd), date('m',$cd)-$mth,
    date('d',$cd)-$day, date('Y',$cd)-$yr));
      return $newdate;
              }

?>

<?php



$sql = "SELECT * ";
$sql .= " FROM `bausme`,`user` ";
$sql .= " WHERE `bausme`.`pabaiga` > '2012-07-01'";
$sql .= " AND `bausme`.`userid` = `user`.`userid`";
$sql .= " AND `user`.`etapas` = 0";
$sql .= " ORDER BY `user`.`lovosid` ASC";


//echo $sql;
echo "<table width=\"100%\" border=\"1\">";
echo "<th>Eil. Nr.</th>";
echo "<th>Bylos nr.</th>";
echo "<th>Vardas</th>";
echo "<th>Pavarde</th>";
echo "<th>Burys</th>";
echo "<th>Brigada</th>";
echo "<th>UDO</th>";
$result = mysql_query($sql, $connection);
$c=1;
while ($row = mysql_fetch_assoc($result)) {

	//iki3
	if (($row['paskmet']<3) ||(($row['paskmet']==3)and($row['paskmen']==0)and($row['paskdien']==0))){
		//echo $c++.". ".$row['userid']." turi ".$row['paskmet']." m. ".$row['paskmen']." men. ".$row['paskdien']." d.<br>";
		//echo $row['pradzia']."<br>";
		$met_in=$row['paskmet'];
		$men_in=$row['paskmen'];
		$die_in=$row['paskdien'];
		$met_out = floor($met_in/3);
		$men_int = $men_in+(($met_in%3)*12);
		$men_out = floor($men_int/3);
		$die_int = $die_in+(($men_in%3)*30);
		$die_out = floor($die_int/3);
		//echo $met_out." m. ". $men_out." men. ". $die_out. " d. <br>"	;
		//echo "UDO:".sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out)."<br>";
		//echo "Koncas:".$row['pabaiga']."<br><br>";
		$udo = sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out);
		if ($udo<'2012-07-01'){
				$sql = "SELECT * ";
				$sql .= " FROM `lovos`,`user` ";
				$sql .= " WHERE `user`.`userid` = ";
				$sql .= $row['userid'];
				$sql .= " AND `user`.`lovosid` = `lovos`.`lovosid`";
				$sql .= " LIMIT 1";
				$res = mysql_query($sql, $connection);
				$lova = mysql_fetch_assoc($res);
			echo "<tr>";
			echo "<td>";
			echo $c++;
			echo "</td>";
			echo "<td>";
			echo $row['bylosnr'];
			echo "</td>";
			echo "<td>";
			echo $row['vardas'];
			echo "</td>";
			echo "<td>";
			echo $row['pavarde'];
			echo "</td>";
			echo "<td>";
			echo $lova['burys'];
			echo "</td>";
			echo "<td>";
			echo $lova['brigada'];
			echo "</td>";
			echo "<td>";
			echo $udo;
			echo "</td>";
			
			echo "</tr>";
		};
	}
	
	
	//nuo 3 iki 10
	elseif(($row['paskmet']>=3)AND($row['paskmet']<=9)OR(($row['paskmet']==10)and($row['paskmen']==0)and($row['paskdien']==0))){
		// echo $c++.". ".$row['userid']." turi ".$row['paskmet']." m. ".$row['paskmen']." men. ".$row['paskdien']." d.<br>";
		// echo $row['pradzia']."<br>";
		$met_in=$row['paskmet'];
		$men_in=$row['paskmen'];
		$die_in=$row['paskdien'];
		$met_out = floor($met_in/2);
		$men_int = $men_in+(($met_in%2)*12);
		$men_out = floor($men_int/2);
		$die_int = $die_in+(($men_in%2)*30);
		$die_out = floor($die_int/2);
		// echo $met_out." m. ". $men_out." men. ". $die_out. " d. <br>"	;
		// echo "UDO:".sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out)."<br>";
		// echo "Koncas:".$row['pabaiga']."<br><br>";
		$udo = sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out);
		if ($udo<'2012-07-01'){
				$sql = "SELECT * ";
				$sql .= " FROM `lovos`,`user` ";
				$sql .= " WHERE `user`.`userid` = ";
				$sql .= $row['userid'];
				$sql .= " AND `user`.`lovosid` = `lovos`.`lovosid`";
				$sql .= " LIMIT 1";
				$res = mysql_query($sql, $connection);
				$lova = mysql_fetch_assoc($res);
			echo "<tr>";
			echo "<td>";
			echo $c++;
			echo "</td>";
			echo "<td>";
			echo $row['bylosnr'];
			echo "</td>";
			echo "<td>";
			echo $row['vardas'];
			echo "</td>";
			echo "<td>";
			echo $row['pavarde'];
			echo "</td>";
			echo "<td>";
			echo $lova['burys'];
			echo "</td>";
			echo "<td>";
			echo $lova['brigada'];
			echo "</td>";
			echo "<td>";
			echo $udo;
			echo "</td>";
			
			echo "</tr>";
		};
	}
	
	
	//nuo 10 iki 15
	elseif(($row['paskmet']>=10)AND($row['paskmet']<=14)OR(($row['paskmet']==15)and($row['paskmen']==0)and($row['paskdien']==0))){
		// echo $c++.". ".$row['userid']." turi ".$row['paskmet']." m. ".$row['paskmen']." men. ".$row['paskdien']." d.<br>";
		
		// echo $row['pradzia']."<br>";
		$met_in=$row['paskmet'];
		$men_in=$row['paskmen'];
		$die_in=$row['paskdien'];
		$met_out = floor($met_in/3);

		$men_int = $men_in+(($met_in%3)*12);
		$men_out = floor($men_int/3);
		$die_int = $die_in+(($men_in%3)*30);
		$die_out = floor($die_int/3);

		// echo $met_out." m. ". $men_out." men. ". $die_out. " d. <br>"	;

	
		// echo "UDO:".sub_date($row['pabaiga'],$die_out,$men_out,$met_out)."<br>";
		// echo "Koncas:".$row['pabaiga']."<br><br>";
	$udo = sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out);
		if ($udo<'2012-07-01'){
				$sql = "SELECT * ";
				$sql .= " FROM `lovos`,`user` ";
				$sql .= " WHERE `user`.`userid` = ";
				$sql .= $row['userid'];
				$sql .= " AND `user`.`lovosid` = `lovos`.`lovosid`";
				$sql .= " LIMIT 1";
				$res = mysql_query($sql, $connection);
				$lova = mysql_fetch_assoc($res);
			echo "<tr>";
			echo "<td>";
			echo $c++;
			echo "</td>";
			echo "<td>";
			echo $row['bylosnr'];
			echo "</td>";
			echo "<td>";
			echo $row['vardas'];
			echo "</td>";
			echo "<td>";
			echo $row['pavarde'];
			echo "</td>";
			echo "<td>";
			echo $lova['burys'];
			echo "</td>";
			echo "<td>";
			echo $lova['brigada'];
			echo "</td>";
			echo "<td>";
			echo $udo;
			echo "</td>";
			
			echo "</tr>";
		};
	}
	
	
	//virs 15
	elseif($row['paskmet']>=15){
		// echo $c++.". ".$row['userid']." turi ".$row['paskmet']." m. ".$row['paskmen']." men. ".$row['paskdien']." d.<br>";
		// echo $row['pradzia']."<br>";
		$met_in=$row['paskmet'];
		$men_in=$row['paskmen'];
		$die_in=$row['paskdien'];
		$met_out = floor($met_in/4);
		$men_int = $men_in+(($met_in%4)*12);
		$men_out = floor($men_int/4);
		$die_int = $die_in+(($men_in%4)*30);
		$die_out = floor($die_int/4);
		// echo $met_out." m. ". $men_out." men. ". $die_out. " d. <br>"	;

		
		// echo "UDO:".sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out)."<br>";
		// echo "Koncas:".$row['pabaiga']."<br><br>";
		$udo = sub_date(sub_date($row['pabaiga'],$die_out,$men_out,$met_out),$die_out,$men_out,$met_out);
		if ($udo<'2012-07-01'){
				$sql = "SELECT * ";
				$sql .= " FROM `lovos`,`user` ";
				$sql .= " WHERE `user`.`userid` = ";
				$sql .= $row['userid'];
				$sql .= " AND `user`.`lovosid` = `lovos`.`lovosid`";
				$sql .= " LIMIT 1";
				$res = mysql_query($sql, $connection);
				$lova = mysql_fetch_assoc($res);
			echo "<tr>";
			echo "<td>";
			echo $c++;
			echo "</td>";
			echo "<td>";
			echo $row['bylosnr'];
			echo "</td>";
			echo "<td>";
			echo $row['vardas'];
			echo "</td>";
			echo "<td>";
			echo $row['pavarde'];
			echo "</td>";
			echo "<td>";
			echo $lova['burys'];
			echo "</td>";
			echo "<td>";
			echo $lova['brigada'];
			echo "</td>";
			echo "<td>";
			echo $udo;
			echo "</td>";
			
			echo "</tr>";
		};
		}
	
	else{
	// echo $c++.". ".$row['userid']." turi ".$row['paskmet']." m. ".$row['paskmen']." men. ".$row['paskdien']." d.<br>";
	};
};
echo "</table>";
?>